﻿using System;
using System.Linq;
using System.Data;

namespace LinqDataSet
{
    class Program
    {
        static void Main(string[] args)
        {

            DataSet ds = buildDataSet();

            // 以下两种方法一样
            DataTable dt = ds.Tables["persons"];
            DataTable dataTable = ds.Tables[0];

            var q = from val in dt.AsEnumerable()
                    select val;

            // item 是 DataRow 类型
            foreach(var item in q)
            {

                Console.WriteLine(item.Field<string>("name"));
            }

            Console.WriteLine(" ");

            var q2 = from val in dataTable.AsEnumerable()
                     select val;
            foreach (var item in q2)
            {

                Console.WriteLine(item.Field<string>("name"));
            }

            Console.WriteLine(" ");

            var q3 = from val in dt.AsEnumerable()
                     select new
                     {
                         Name = val.Field<string>("name"),
                         Age = val.Field<int>("age"),
                         Addr = val.Field<string>("addr")
                     };

            foreach (var item in q3)
            {

                Console.WriteLine(item.Name);
            }

            Console.WriteLine(" ");

           


            Console.WriteLine("Hello World!");
        }


        public static DataSet buildDataSet()
        {
            DataSet ds = new DataSet("persons");
            DataTable dt = new DataTable("persons");

            dt.Columns.Add(new DataColumn("name",Type.GetType("System.String")));
            dt.Columns.Add(new DataColumn("age",Type.GetType("System.Int32")));
            dt.Columns.Add(new DataColumn("sex",Type.GetType("System.String")));
            dt.Columns.Add(new DataColumn("addr",Type.GetType("System.String")));

            dt.Rows.Add("yejing",32,"男","guzhou");
            dt.Rows.Add("zhangshan",33,"男", "liaoling");
            dt.Rows.Add("lisi",20,"女","shanxi");
            dt.Rows.Add("wangwu",19,"女","xian");


            ds.Tables.Add(dt);


            return ds;
        }
       

    }


    class Person
    {

        private string _name;
        private int _age;
        private string _addr;
        private string _sex;

        public Person(string name, int age, string sex,string addr)
        {
            Name = name;
            Age = age;
            Addr = addr;
            Sex = sex;
        }

        public string Name
        {
            get
            {
                return _name;
            }
            set
            {
                _name = value;
            }
        }

        public int Age
        {
            get => _age;
            set => _age = value;
        }

        public string Addr
        {
            get => _addr;
            set => _addr = value;
        }

        public string Sex { get => _sex; set=> _sex = value; }
    }
}
